home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / gs24src.zip / GSPATH2.C < prev    next >
C/C++ Source or Header  |  1992-03-24  |  7KB  |  232 lines

  1. /* Copyright (C) 1989, 1992 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gspath2.c */
  21. /* Non-constructor path routines for GhostScript library */
  22. #include "gx.h"
  23. #include "gserrors.h"
  24. #include "gspath.h"
  25. #include "gxfixed.h"
  26. #include "gxarith.h"
  27. #include "gxmatrix.h"
  28. #include "gzstate.h"
  29. #include "gzpath.h"
  30. #include "gzdevice.h"
  31.  
  32. /* Forward references */
  33. private int common_clip(P2(gs_state *, int));
  34. private int set_clip_path(P3(gs_state *, gx_clip_path *, int));
  35.  
  36. /* Path enumeration structure */
  37. struct gs_path_enum_s {
  38.     segment *pseg;
  39.     const gs_state *pgs;
  40. };
  41.  
  42. /* Size of path enumeration structure, so clients can allocate */
  43. const uint gs_path_enum_sizeof = sizeof(gs_path_enum);
  44.  
  45. /* ------ Path transformers ------ */
  46.  
  47. int
  48. gs_flattenpath(gs_state *pgs)
  49. {    gx_path fpath;
  50.     int code;
  51.     if ( !pgs->path->curve_count ) return 0;    /* no curves */
  52.     code = gx_path_flatten(pgs->path, &fpath, pgs->flatness);
  53.     if ( code < 0 ) return code;
  54.     gx_path_release(pgs->path);
  55.     *pgs->path = fpath;
  56.     return 0;
  57. }
  58.  
  59. int
  60. gs_reversepath(gs_state *pgs)
  61. {    gx_path rpath;
  62.     int code = gx_path_reverse(pgs->path, &rpath);
  63.     if ( code < 0 ) return code;
  64.     gx_path_release(pgs->path);
  65.     *pgs->path = rpath;
  66.     return 0;
  67. }
  68.  
  69. /* ------ Accessors ------ */
  70.  
  71. int
  72. gs_pathbbox(gs_state *pgs, gs_rect *pbox)
  73. {    gs_fixed_rect fbox;        /* box in device coordinates */
  74.     gs_rect dbox;
  75.     int code = gx_path_bbox(pgs->path, &fbox);
  76.     if ( code < 0 ) return code;
  77.     /* Transform the result back to user coordinates. */
  78.     dbox.p.x = fixed2float(fbox.p.x);
  79.     dbox.p.y = fixed2float(fbox.p.y);
  80.     dbox.q.x = fixed2float(fbox.q.x);
  81.     dbox.q.y = fixed2float(fbox.q.y);
  82.     return gs_bbox_transform_inverse(&dbox, &ctm_only(pgs), pbox);
  83. }
  84.  
  85. /* ------ Enumerators ------ */
  86.  
  87. /* Start enumerating a path */
  88. void
  89. gs_path_enum_init(gs_path_enum *penum, const gs_state *pgs)
  90. {    penum->pseg = (segment *)pgs->path->first_subpath;
  91.     penum->pgs = pgs;
  92. }
  93.  
  94. /* Enumerate the next element of a path. */
  95. /* If the path is finished, return 0; */
  96. /* otherwise, return the element type. */
  97. int
  98. gs_path_enum_next(gs_path_enum *penum, gs_point ppts[3])
  99. {    segment *pseg = penum->pseg;
  100.     const gs_state *pgs = penum->pgs;
  101.     gs_point pt;
  102.     int code;
  103.     if ( pseg == 0 ) return 0;    /* finished */
  104.     penum->pseg = pseg->next;
  105.     if ( pseg->type == s_line_close )
  106.       return gs_pe_closepath;
  107.     if ( (code = gs_itransform(pgs, fixed2float(pseg->pt.x),
  108.                    fixed2float(pseg->pt.y), &pt)) < 0 )
  109.       return code;
  110.     switch ( pseg->type )
  111.        {
  112.     case s_start:
  113.          ppts[0] = pt;
  114.          return gs_pe_moveto;
  115.     case s_line:
  116.          ppts[0] = pt;
  117.          return gs_pe_lineto;
  118.     case s_curve:
  119. #define pcurve ((curve_segment *)pseg)
  120.          if ( (code =
  121.            gs_itransform(pgs, fixed2float(pcurve->p1.x),
  122.                  fixed2float(pcurve->p1.y), &ppts[0])) < 0 ||
  123.           (code =
  124.            gs_itransform(pgs, fixed2float(pcurve->p2.x),
  125.                  fixed2float(pcurve->p2.y), &ppts[1])) < 0 )
  126.            return 0;
  127.          ppts[2] = pt;
  128.          return gs_pe_curveto;
  129. #undef pcurve
  130.     default:
  131.          lprintf1("bad type %x in gs_path_enum_next!\n", pseg->type);
  132.          gs_exit(1);
  133.        }
  134. }
  135.  
  136. /* ------ Clipping ------ */
  137.  
  138. int
  139. gs_clippath(gs_state *pgs)
  140. {    gx_path path;
  141.     int code = gx_cpath_path(pgs->clip_path, &path);
  142.     if ( code < 0 ) return code;
  143.     return gx_path_copy(&path, pgs->path);
  144. }
  145.  
  146. int
  147. gs_initclip(gs_state *pgs)
  148. {    register gx_device *dev = pgs->device->info;
  149.     gs_fixed_rect box;
  150.     if ( is_fzero2(dev->l_margin, dev->r_margin) &&
  151.          is_fzero2(dev->b_margin, dev->t_margin)
  152.        )
  153.        {    /* Shortcut, don't need to worry about density. */
  154.         box.p.x = box.p.y = 0;
  155.         box.q.x = int2fixed(dev->width);
  156.         box.q.y = int2fixed(dev->height);
  157.        }
  158.     else
  159.        {    /* Indent from bounding rectangle. */
  160.         gs_matrix_fixed imat;
  161.         (*dev->procs->get_initial_matrix)(dev, (gs_matrix *)&imat);
  162.         gs_update_matrix_fixed(&imat);
  163.         gs_point_transform2fixed(&imat,
  164.                      dev->l_margin * 72,
  165.                      dev->b_margin * 72,
  166.                      &box.p);
  167.         gs_point_transform2fixed(&imat,
  168.                      (dev->width / dev->x_pixels_per_inch - dev->r_margin) * 72,
  169.                      (dev->height / dev->y_pixels_per_inch - dev->t_margin) * 72,
  170.                      &box.q);
  171.        }
  172.     return gx_clip_to_rectangle(pgs, &box);
  173. }
  174.  
  175. int
  176. gs_clip(gs_state *pgs)
  177. {    return common_clip(pgs, gx_rule_winding_number);
  178. }
  179.  
  180. int
  181. gs_eoclip(gs_state *pgs)
  182. {    return common_clip(pgs, gx_rule_even_odd);
  183. }
  184.  
  185. private int
  186. common_clip(gs_state *pgs, int rule)
  187. {    gx_path fpath;
  188.     int code = gx_path_flatten(pgs->path, &fpath, pgs->flatness);
  189.     if ( code < 0 ) return code;
  190.     code = gx_cpath_intersect(pgs, pgs->clip_path, &fpath, rule);
  191.     if ( code < 0 ) return code;
  192.     return set_clip_path(pgs, pgs->clip_path, rule);
  193. }
  194.  
  195. /* Establish a rectangle as the clipping path. */
  196. /* Used by initclip and by the character cache logic. */
  197. int
  198. gx_clip_to_rectangle(gs_state *pgs, gs_fixed_rect *pbox)
  199. {    gx_clip_path cpath;
  200.     int code = gx_cpath_from_rectangle(&cpath, pbox, &pgs->memory_procs);
  201.     if ( code < 0 ) return code;
  202.     gx_cpath_release(pgs->clip_path);
  203.     return set_clip_path(pgs, &cpath, gx_rule_winding_number);
  204. }
  205.  
  206. /* Set the clipping path to the current path, without intersecting. */
  207. /* Currently only used by the insideness testing operators, */
  208. /* but might be used by viewclip eventually. */
  209. /* The algorithm is very inefficient; we'll improve it later if needed. */
  210. int
  211. gx_clip_to_path(gs_state *pgs)
  212. {    gs_fixed_rect bbox;
  213.     int code;
  214.     (code = gx_path_bbox(pgs->path, &bbox)) < 0 ||
  215.     (code = gx_clip_to_rectangle(pgs, &bbox)) < 0 ||
  216.     (code = gs_clip(pgs));
  217.     return code;
  218. }
  219.  
  220. /* Set the clipping path (internal). */
  221. private int
  222. set_clip_path(gs_state *pgs, gx_clip_path *pcpath, int rule)
  223. {    *pgs->clip_path = *pcpath;
  224.     pgs->clip_rule = rule;
  225. #ifdef DEBUG
  226. if ( gs_debug['p'] )
  227.     dprintf("[p]Clipping path:\n"),
  228.     gx_cpath_print(pcpath);
  229. #endif
  230.     return 0;
  231. }
  232.